package com.qumitech.visualnovel.model.dao.console;

import com.alibaba.fastjson.JSONObject;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.DecimalFormat;

public class RemainUserCount {
    private Integer id;

    private String appId;

    private Date targetDate;

    private String appPackage;

    private String appVersion;

    private String channel;

    private Integer activateCount;

    private Integer nextCount;

    private Integer threeCount;

    private Integer sevenCount;

    private Integer fourteenCount;

    private Integer thirtyCount;

    private Date updatedAt;

    private Date createdAt;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId == null ? null : appId.trim();
    }

    public Date getTargetDate() {
        return targetDate;
    }

    public void setTargetDate(Date targetDate) {
        this.targetDate = targetDate;
    }

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appName) {
        this.appVersion = appName == null ? null : appName.trim();
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel == null ? null : channel.trim();
    }

    public Integer getActivateCount() {
        return activateCount;
    }

    public void setActivateCount(Integer activateCount) {
        this.activateCount = activateCount;
    }

    public Integer getNextCount() {
        return nextCount;
    }

    public void setNextCount(Integer nextCount) {
        this.nextCount = nextCount;
    }

    public Integer getThreeCount() {
        return threeCount;
    }

    public void setThreeCount(Integer threeCount) {
        this.threeCount = threeCount;
    }

    public Integer getSevenCount() {
        return sevenCount;
    }

    public void setSevenCount(Integer sevenCount) {
        this.sevenCount = sevenCount;
    }

    public Integer getFourteenCount() {
        return fourteenCount;
    }

    public void setFourteenCount(Integer fourteenCount) {
        this.fourteenCount = fourteenCount;
    }

    public Integer getThirtyCount() {
        return thirtyCount;
    }

    public void setThirtyCount(Integer thirtyCount) {
        this.thirtyCount = thirtyCount;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", appId=").append(appId);
        sb.append(", targetDate=").append(targetDate);
        sb.append(", appVersion=").append(appVersion);
        sb.append(", channel=").append(channel);
        sb.append(", activateCount=").append(activateCount);
        sb.append(", nextCount=").append(nextCount);
        sb.append(", threeCount=").append(threeCount);
        sb.append(", sevenCount=").append(sevenCount);
        sb.append(", fourteenCount=").append(fourteenCount);
        sb.append(", thirtyCount=").append(thirtyCount);
        sb.append(", updatedAt=").append(updatedAt);
        sb.append(", createdAt=").append(createdAt);
        sb.append("]");
        return sb.toString();
    }
    public Integer formatData(int type)
    {
        Integer data = 0;
        if(type>=5 && getActivateCount()==0)
        {
            return data;
        }
        switch (type)
        {
            case 0:data = getNextCount();break;
            case 1:data = getThreeCount();break;
            case 2:data = getSevenCount();break;
            case 3:data = getFourteenCount();break;
            case 4:data = getThirtyCount();break;
            case 5:data = getNextCount()*100/getActivateCount();break;
            case 6:data = getThreeCount()*100/getActivateCount();break;
            case 7:data = getSevenCount()*100/getActivateCount();break;
            case 8:data = getFourteenCount()*100/getActivateCount();break;
            case 9:data = getThirtyCount()*100/getActivateCount();break;
        }
        return data;
    }

    public String formatSringData(int type)
    {
        String data = "0";
        if(type>=5 && getActivateCount()==0)
        {
            return data;
        }
        DecimalFormat dcmFormat = new DecimalFormat("0.00");
        switch (type)
        {
            case 0:data = getNextCount().toString();break;
            case 1:data = getThreeCount().toString();break;
            case 2:data = getSevenCount().toString();break;
            case 3:data = getFourteenCount().toString();break;
            case 4:data = getThirtyCount().toString();break;
            case 5:data = dcmFormat.format(getNextCount()*100.0/getActivateCount());break;
            case 6:data = dcmFormat.format(getThreeCount()*100.0/getActivateCount());break;
            case 7:data = dcmFormat.format(getSevenCount()*100.0/getActivateCount());break;
            case 8:data = dcmFormat.format(getFourteenCount()*100.0/getActivateCount());break;
            case 9:data = dcmFormat.format(getThirtyCount()*100.0/getActivateCount());break;
        }
        return data;
    }

    public String showStr(int n,int k) {
        Date tempDate = getDeteBydelta(n);
        Date currentDate = new Date();
        if (tempDate.getTime() > currentDate.getTime()) {
            return "-";
        }
        else{
            return formatSringData(5+k) + "%("+formatSringData(k)+ ")";
        }
    }

    public Date getDeteBydelta(int n)
    {
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(targetDate);
        calendar.add(calendar.DATE,n);//把日期往后增加一天.整数往后推,负数往前移动
        Date date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        return date;
    }

    public JSONObject formatJson()
    {
        JSONObject itemData = new JSONObject();
        itemData.put("appId", appId);
        itemData.put("appPackage", appPackage);
        itemData.put("channel", channel);
        itemData.put("appVersion", appVersion);
        itemData.put("targetDate", targetDate);
        itemData.put("nextCount", showStr(1,0));
        itemData.put("threeCount", showStr(3,1));
        itemData.put("sevenCount", showStr(7,2));
        itemData.put("fourteenCount", showStr(14,3));
        itemData.put("thirtyCount", showStr(30,4));
        itemData.put("activateCount", activateCount);
        return itemData;
    }


    public String getAppPackage() {
        return appPackage;
    }

    public void setAppPackage(String appPackage) {
        this.appPackage = appPackage;
    }
}